import 'package:flutter/material.dart';
import 'package:flutter_pickers/time_picker/model/date_mode.dart';
import 'package:flutter_pickers/time_picker/model/pduration.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/account/api/accountApi.dart';
import 'package:honeypotflutter/modules/account/view/account_withdraw_history_Item.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_date.dart';
import 'package:honeypotflutter/publicBussine/bussine_font.dart';
import 'package:honeypotflutter/publicBussine/bussine_image.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_card.dart';
import 'package:honeypotflutter/public/views/public_appBar.dart';
import 'package:honeypotflutter/public/public_picker.dart';
import 'package:honeypotflutter/public/views/public_refresh.dart';
import '../../model/account_withdraw_history_model.dart';
import 'logic.dart';

class AccountWithdrawHistoryPage extends StatefulWidget {
  const AccountWithdrawHistoryPage({Key? key}) : super(key: key);

  @override
  State<AccountWithdrawHistoryPage> createState() => _AccountWithdrawHistoryPageState();
}

class _AccountWithdrawHistoryPageState extends State<AccountWithdrawHistoryPage> {
  final AccountWithdrawHistoryLogic logic = Get.put(AccountWithdrawHistoryLogic());
  final HLRefreshController _refreshController = HLRefreshController(initialRefresh: true);

  /*
  * 选择时间
  * */
  selectDatePickView() {
    HLPicker.showDatePicker(
      mode: DateMode.YM,
      maxDate: PDuration.now(),
      onConfirm: (PDuration res) {
        BussineSelectDateModel date = MGBussineDate.calculateDate(yy: res.year, mm: res.month);
        logic.startTime = date.startTime;
        logic.endTime = date.endTime;
        logic.currentTime.value = date.currentTime;
        _refreshController.requestRefresh();
      },
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    BussineSelectDateModel date = MGBussineDate.calculateDate();
    logic.startTime = date.startTime;
    logic.endTime = date.endTime;
    logic.currentTime.value = date.currentTime;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HLAppBar(navigationTitle: "提现记录"),
      body: Column(
        children: [
          MGCard(
            margin: EdgeInsets.only(left: 12, right: 12, top: 10, bottom: 10),
            padding: EdgeInsets.only(top: 20, bottom: 20),
            children: [
              Row(
                children: [
                  Obx(() => Text(
                        logic.currentTime.value,
                        style: TextStyle(
                          color: MGColors.text1Color,
                          fontWeight: FontWeight.bold,
                          fontSize: MGFont.titleFont2,
                        ),
                      )),
                  Image.asset(
                    MGImage.image("account_down", type: ImagePathType.account),
                    width: 15,
                  )
                ],
                mainAxisAlignment: MainAxisAlignment.center,
              )
            ],
            action: selectDatePickView,
          ),
          Expanded(
              child: HLRefresh<AccountWithdrawHistoryItemModel>(
            refreshController: _refreshController,
            onLoading: () async {
              logic.page += 1;
              return await AccountApi.withdrawHistory(logic.startTime, logic.endTime, logic.page);
            },
            onRefresh: () async {
              logic.page = 1;
              return await AccountApi.withdrawHistory(logic.startTime, logic.endTime, logic.page);
            },
            padding: EdgeInsets.only(left: 12, right: 12),
            snap: (data, int index) {
              return AccountWithdrawHistoryItem(itemModel: data);
            },
          ))
        ],
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<AccountWithdrawHistoryLogic>();
    super.dispose();
  }
}
